# -*- coding: utf-8 -*-

import asyncio, time, logging
from pydl import api, utils, m3u8, download

def main_file():
    from pydl.logger import getLogger
    getLogger()
    # url = "https://la3.killcovid2021.com/m3u8/907823/907823.m3u8"
    # url = "https://askzycdn.com/20231124/X55udrAS/2000kb/hls/index.m3u8"
    url = "https://la3.killcovid2021.com/m3u8/907823/9078230.ts"
    outputPath = f"dist/{utils.resource_name(url)}"
    start_time = time.time()
    asyncio.run(api.download(url, outputPath, download.new_file_infos, proxy="socks5://127.0.0.1:1080", verbose=True))
    logging.info(f"程序执行时间为 {time.time()-start_time} 秒")

def main_m3u8():
    from pydl.logger import getLogger
    getLogger()
    # url = "https://la3.killcovid2021.com/m3u8/907823/907823.m3u8"
    # url = "https://askzycdn.com/20231124/X55udrAS/2000kb/hls/index.m3u8"
    # url = "https://vip3.lbbf9.com/20231129/9PyUSSFA/index.m3u8"
    # url = "https://vip3.lbbf9.com/20231129/9PyUSSFA//700kb/hls/index.m3u8"	
    # url = "https://video56.zdavsp.com/video/20230613/6ab714fed9a9cb653d6eeec3937b70d6/index.m3u8"
    # url = "https://videozmwbf.0afaf5e.com/decry/vd/20231126/MDZhZmU0ND/151813/720/libx/hls/encrypt/index.m3u8"
    url = "https://la3.killcovid2021.com/m3u8/907759/907759.m3u8"
    # url = "https://jkunbf.com/20240109/893YFIza/2000kb/hls/index.m3u8"
    url = "https://javday.online/videos/64688cc472d260c5fab88663/gaa200/index.m3u8"
    outputPath = f"dist/{utils.resource_path(url)}"

    # url = "https://hjcca9.top/api/address/6a5606567d65cf8c0860c568e2f58d33?t=cWeLa7KLCirh64hiq7TcCnp_bogGNdtuDHa8oKUV6FO9_qGgtqZszgeKmYsth2KUfVKY3VbDCAtH8FEVY9Il5nlan_a2zS51yg59UW6Fw-XZTIu8tlYZI8tuDYe7d4EFv9p_bmjVdZEWNa-tpCSZTyDrn5PYgLOLZCbabd29qzsLzCFtpxgQEpkHNy7ymClfwcFhkrp4b3jeebqQ1zhWHJ90Ri5oMaQ7hhCZuCSdcyHEZcdg61iX1NWeF5frDUjD"
    # outputPath = f"dist/hjcca9.top/api/address/6a5606567d65cf8c0860c568e2f58d33/7409255.m3u8"
    # url = "https://hjcca9.top/api/address/56094af9abee4b7b784a392dffaa8b88?t=0E_HCfSjgFeP72cKk5DH09ERl_XVFfa5bHcnTOzVFWPzXDs7TaId6y1BARQxNmS9nck0-8u4lgPzizx6v9bDRHlan_a2zS51yg59UW6Fw-UCZRr7wpmttBgeuCXKX3sgq0ogo3fJ4bxs6JIO3Gc7uLxdfdYCI9-f93tQ3DZeADZRyYnxws4UxGF4npLGoUZUwcFhkrp4b3jeebqQ1zhWHJ90Ri5oMaQ7hhCZuCSdcyHEZcdg61iX1NWeF5frDUjD"
    # outputPath = f"dist/hjcca9.top/api/address/56094af9abee4b7b784a392dffaa8b88/7356222.m3u8"


    start_time = time.time()
    proxy = None
    # proxy = 'socks5://127.0.0.1:1080'
    result = asyncio.run(api.download(url, outputPath, m3u8.new_m3u8_infos, proxy=proxy, headers=headers, verbose=True))
    logging.info(f"result: {result}")
    logging.info(f"程序执行时间为 {time.time()-start_time} 秒")


headers = [
    {
        "name": "Accept",
        "value": "*/*"
    },
    {
        "name": "Accept-Encoding",
        "value": "gzip, deflate, br, zstd"
    },
    {
        "name": "Accept-Language",
        "value": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2"
    },
    {
        "name": "Connection",
        "value": "keep-alive"
    },
    {
        "name": "Host",
        "value": "javday.online"
    },
    {
        "name": "Origin",
        "value": "https://javday.tv"
    },
    {
        "name": "Referer",
        "value": "https://javday.tv/"
    },
    {
        "name": "Sec-Fetch-Dest",
        "value": "empty"
    },
    {
        "name": "Sec-Fetch-Mode",
        "value": "cors"
    },
    {
        "name": "Sec-Fetch-Site",
        "value": "cross-site"
    },
    {
        "name": "User-Agent",
        "value": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:126.0) Gecko/20100101 Firefox/126.0"
    }
]
headers = {item["name"]: item["value"] for item in headers}

def parse_m3u8():
    from pydl.logger import getLogger
    getLogger()
    # url = "https://la3.killcovid2021.com/m3u8/907823/907823.m3u8"
    # url = "https://askzycdn.com/20231124/X55udrAS/2000kb/hls/index.m3u8"
    url = "https://vip3.lbbf9.com/20231129/9PyUSSFA/index.m3u8"
    url = "https://jkunbf.com/20240109/893YFIza/2000kb/hls/index.m3u8"
    url = "https://javday.online/videos/64688cc472d260c5fab88663/gaa200/index.m3u8"
    # https://javday.tv/category/txvlog/
    outputPath = f"dist/{utils.resource_path(url)}"
    # url = "https://hjcca9.top/api/address/6a5606567d65cf8c0860c568e2f58d33?t=cWeLa7KLCirh64hiq7TcCnp_bogGNdtuDHa8oKUV6FO9_qGgtqZszgeKmYsth2KUfVKY3VbDCAtH8FEVY9Il5nlan_a2zS51yg59UW6Fw-XZTIu8tlYZI8tuDYe7d4EFv9p_bmjVdZEWNa-tpCSZTyDrn5PYgLOLZCbabd29qzsLzCFtpxgQEpkHNy7ymClfwcFhkrp4b3jeebqQ1zhWHJ90Ri5oMaQ7hhCZuCSdcyHEZcdg61iX1NWeF5frDUjD"
    # outputPath = f"dist/hjcca9.top/api/address/6a5606567d65cf8c0860c568e2f58d33/7409255.m3u8"
    start_time = time.time()


    async def _parse():
        async with api.get_session(headers=headers) as session:
            result = []
            await m3u8.parse_m3u8(url, outputPath, session, result)
            print(result)
    asyncio.run(_parse())
    logging.info(f"程序执行时间为 {time.time()-start_time} 秒")
if __name__ == '__main__':
    import json
    print(json.dumps(headers))
    # parse_m3u8()
    # main_m3u8()
    # main_file()